/************ JunrLi_sbus control ************/
#include "remote_control.h"
#include "main.h"
#include "stdint.h"

RC_ctrl_t rc_ctrl;

uint8_t SBUS_MultiRx_Buf[2][RC_FRAME_LENGTH];

void SBUS_TO_RC(volatile const uint8_t *sbus_buf, RC_ctrl_t *rc_ctrl)
{
    if(sbus_buf[0]==0x0f&&sbus_buf[24]==0x00)
    {
        rc_ctrl->rc.ch[0]  = ((sbus_buf[1]    |sbus_buf[2]<<8)                   &0x07FF);
        rc_ctrl->rc.ch[1]  = ((sbus_buf[2]>>3 |sbus_buf[3]<<5)                   &0x07FF);
        rc_ctrl->rc.ch[3]  = ((sbus_buf[3]>>6 |sbus_buf[4]<<2  |sbus_buf[5]<<10) &0x07FF);//对调为了通道匹配dt7
        rc_ctrl->rc.ch[2]  = ((sbus_buf[5]>>1 |sbus_buf[6]<<7)                   &0x07FF);
        rc_ctrl->rc.ch[4]  = ((sbus_buf[6]>>4 |sbus_buf[7]<<4)                   &0x07FF);
        rc_ctrl->rc.ch[5]  = ((sbus_buf[7]>>7 |sbus_buf[8]<<1  |sbus_buf[9]<<9)  &0x07FF);
        rc_ctrl->rc.ch[6]  = ((sbus_buf[9]>>2 |sbus_buf[10]<<6)                  &0x07FF);
        rc_ctrl->rc.ch[7]  = ((sbus_buf[10]>>5|sbus_buf[11]<<3)                  &0x07FF);
        rc_ctrl->rc.ch[8]  = ((sbus_buf[12]   |sbus_buf[13]<<8)                  &0x07FF);
        rc_ctrl->rc.ch[9]  = ((sbus_buf[13]>>3|sbus_buf[14]<<5)                  &0x07FF);
        rc_ctrl->rc.ch[10] = ((sbus_buf[14]>>6|sbus_buf[15]<<2 |sbus_buf[16]<<10)&0x07FF);
        rc_ctrl->rc.ch[11] = ((sbus_buf[16]>>1|sbus_buf[17]<<7)                  &0x07FF);
        rc_ctrl->rc.ch[12] = ((sbus_buf[17]>>4|sbus_buf[18]<<4)                  &0x07FF);
        rc_ctrl->rc.ch[13] = ((sbus_buf[18]>>7|sbus_buf[19]<<1 |sbus_buf[16]<<10)&0x07FF);
        rc_ctrl->rc.ch[14] = ((sbus_buf[20]>>2|sbus_buf[21]<<6)                  &0x07FF);
        rc_ctrl->rc.ch[15] = ((sbus_buf[21]>>5|sbus_buf[22]<<3)                  &0x07FF);
        
        rc_ctrl->rc.ch[0] -= RC_CH_VALUE_OFFSET;
        rc_ctrl->rc.ch[1] -= RC_CH_VALUE_OFFSET;
        rc_ctrl->rc.ch[2] -= RC_CH_VALUE_OFFSET;
        rc_ctrl->rc.ch[3] -= RC_CH_VALUE_OFFSET;
        rc_ctrl->rc.ch[4] -= RC_CH_VALUE_OFFSET;
    }
    else
    {
        rc_ctrl->rc.ch[0] = 404;
        rc_ctrl->rc.ch[1] = 404;
    }
}

